
const api = {
  //用户相关
  inserUserChannel: "/userChannel/put/inserUserChannel",//渠道统计
  login: "/user/login", //登录
  getToken: "/tuser/put/loginUser",//用户登录
  login_ux: "/tuser/put/getTokenFromWXByCodeForUXuan",//登录优选
  updateUserInfo: "/tuser/updateUserInfo",//更新用户信息
  get_userInfo: "/tuser/get/userInfo",//用户信息
  user_unionId: "/tuser/put/userUnionId",
  send_message: "/wx/template/put/sendTemplateMeg",//发送消息
  ranking: "/userAccountDetail/get/ranking",//果冻王
  saveUserFormId: "/wx/template/save/saveUserFormId",//保存用户表单id 用于推送服务消息
  //首页公告
  advertising:"/notice/get/indexNotice",
  message_num:"/sysmsg/get/urMsgTotal",
  //设置
  get_settings: "/tuser/get/mySetting",//获取设置信息,
  send_code: "/tuser/put/verificationCode",//发送验证码
  bind_phone: "/tuser/put/bindingMobile",// 绑定手机号
  check_code: "/user/settings/checkCode",// 校验验证码
  reset_password: "/user/settings/resetPassword", //重置密码
  wallet_setting: "/setting/get/wallet",//钱包配置
  //轮播图
  pic_settings: "/setting/list/picSettings",
  //分享
  share_setting: "/setting/get/shareSetting",
  //商品想关
  search_goods: "/goods/search",//商品模糊查询
  goods_can_buy_count: "/goods/get/goods_count/user_can_buy",//获取可购买商品的数量
  goods_can_buy: "/goods/get/goods/user_can_buy",//获取可购买商品列表
  goods_friend_put: "/goods/followerList",//获取好友发布的商品列表
  goods_list: "/goods/list", //商品列表 
  brand_list: "/brand/getBrandList", //品牌列表
  goods_list_orders: "/goods/list/orders", //已经领取到商品的人
  task_list: "/task/list", //获取人物列表信息
  goods_del: "/goods/del", //删除商品
  goods_put_reward: "/goods/put/reward", //打赏商品
  goods_put_all: "/goods/put_all", //上传商品信息 所有数据
  goods_put_data: "/goods/put_data", //上传商品信息数据
  goods_put_updateGoods: "/goods/put/updateGoods",
  goods_put_files: "/goods/put_files", //上传商品信息文件
  task_insertUserTask: "/task/insertUserTask", //插入任务
  task_signInDetail: "/task/signInDetail", //签到领取果冻详情页面
  //评论
  box: "/card-service-web/activity/checkChestBoxs",
  comment_all: "/comment/get/page",
  comment_add: "/comment/put",
  comment_del: "/comment/del",
  //送书
  send_book_detail: "/giftbook/get", //送书活动详情
  send_book_list: "/giftbook/get/page",// 送书列表
  send_book_submit_order: "/giftbook/put",//送书订单
  send_book_activity: "/giftbook/get/activity",//送书活动内容
  send_book_love_record: "/giftbook/get/bookUser",//送书爱心记录,
  //闪购
  groupbuy_list: "/groupbuy/list",//列表
  groupbuy_detail: "/groupbuy/get/goodsDetail",//详情
  openGroupsBuy: "/groupbuy/get/openGroupsBuy",//开团
  goods_list_more: "/goods/list/more",//更多相关商品
  groupbuy_join: "/groupbuy/put/joinGroupBuy",
  groupbuy_rob: "/groupbuy/put/rob",
  //拼团
  groupbuy_list_book: "/groupbuy/list/booking",//拼团列表
  groupbuy_list_bookDetail: "/groupbuy/get/bookingDetail",//拼团商品详情
  join_groupbuy_book: "/groupbuy/put/helpGrouper",//参与拼团
  groupbuy_countersign: "/groupbuy/put/countersign",//订单信息
  groupbuy_commit: "/groupbuy/put/groupConfirm",//提交订单
  groupbuy_groupDetail: "/groupbuy/get/groupDetail",//拼团详情
  //红包拼团
  groupbuy_new_list: "/groupbuy/list/newBooking",//拼团列表
  groupbuy_confirm_confirm_redpacket: "/groupbuy/put/groupConfirmRedPacket",//团长开团
  groupbuy_help_grouper_redpacket: "/groupbuy/put/helpGrouperRedPacket",//参与拼团
  groupbuy_detail_new: "/groupbuy/get/groupDetailNew",//拼团红包详情 ;
  groupBuy_my_redpacket_record: "/groupbuy/get/receive/groupbuypacket/list",//当前用户的红包记录
  groupbuy_user_status: "/groupbuy/get/check/groupbuypacket",//用户在当前团中的状态
  // 现金红包
  groupbuy_packetdetail: "/groupbuy/get/groupbuypacketdetail",// 当前用户的红包拼团详情
  groupbuy_receive_packet: "/groupbuy/get/receive/groupbuypacket", // 当前用户的红包拼团领取
  groupbuy_list_packet: "/groupbuy/get/groupbuypacket",// 红包拼团详情列表

  //订单详情
  order_details: '/goods/get/userOrderDetail',
  get_fans_page: "/userfans/get/fans/page",//根据用户ID获取此用户的粉丝信息
  order_userlist: "/order/list/userList",//获取单个用户订单列表信息
  comfire_send_goods: "/order/put/sendGoods",//确认发货
  collect_goods:"/order/put/comfireGoods",  //确认收货
  up_ordetails:"/order/put/modifyOrder",         // 修改订单
  pre_pay:"/wx/put/pay",//微信预支付
  //我的
  mine_saveSupplier: "/supplier/saveSupplier",//商家入驻
  obtain_success: "/goods/list/orders", //获取已领取该商品的人
  goods_details: "/goods/get",          //根据商品ID获取该商品详情
  praise_candy: "/goods/put/reward",  //打赏商品
  redeemcode: "/redeemcode/put/code",//积分兑换

  //获取用户发布信息
  getInformation: "/tuser/getInformation",
  user_fav_goods: "/store/get/goods", //用户收藏商品
  user_fav_story: "/store/get/story", //用户收藏故事
  user_sys_msg: "/sysmsg/get/page", //用户系统消息
  user_notice_details:"/notice/get/noticeInfo",
  user_list_msg: "/chatroom/list",  //用户聊天列表
  user_unread_num: "/chatroom/get/totalUnReadNum",//未读聊天消息数量

  user_list_notice:"/notice/get/noticeList",
  readNotice: "/notice/get/readNotice",
  
  user_list_sys_unread_num: "/sysmsg/get/unReadMsgNum",

  user_msg_records: "/chatmessage/list/messages",  //获取房间消息记录
  user__msg_put: "/chatmessage/put",  //发送聊天消息
  user__msg_room_put: "/chatroom/put",  //创建聊天房间
  user_fav: "/store/put", //用户收藏
  user_like: "/admire/put", //用户点赞
  user_fav_del: "/store/put/update", //取消收藏
  user_address: "/tuser/list/address", //获取用户地址
  user_add_address: "/tuser/put/insertAddress", //用户新增地址
  user_delete_address: "/tuser/del/deleteAddress", //用户删除地址
  reward: "/goods/list/page/reward",             //根据商品ID获取商品的所有打赏
  goods_more_list: "/goods/list/more",       //获取相关更多的商品
  userfans_get_page: "/userfans/get/page", //根据当前用户ID获取关注用户
  test_list: "/task/list",  //获取任务列表信息
  user_aution_list: "/auction/get/page", //用户竞拍信息
  goods_list_userOrder: "/goods/list/userOrder", //根据状态获取用户领取的宝贝信息
  // 故事
  story_list: "/story/get/story/list",//查询故事列表
  add_story: "/story/put",//发表感谢视频
  cut_goods_records: "/goods/put/cutGoodsRecords",//砍价记录
  cut_goods: "/goods/cutGoods",                //帮助好友砍商品
  //签到
  signin_detail:"/task/put/signInDetail",
  signin:"/task/put/signIn",

  goods_list_userList: "/goods/list/userList",//获取单个用户商品列表信息 用于我送出的-赠送中、 用户中心-分享宝贝
  user_follower: "/userfollower/list",  // 用户可能认识的人
  story_details: "/story/get/story/detail", //故事信息详情查询
  upload_file: "/upload",//上传文件
  goods_dropoff: '/goods/dropoff',  // 下架商品
  goods_dropon: '/goods/dropon', // 重新上架
  // 详情页面
  default_pay:"/goods/put/buy",
  userfans_get_fans: "/userfans/get/fans", //当前用户是否已关注过目标用户，0没有，1已关注
  get_me_fans: "/userfans/get/me/fans/page", //根据当前用户ID获取粉丝信息
  put_fans: "/userfans/put/fans", //增加或修改关注用户
  get_admire: "/admire/get/admire",//查询当前用户是否对此商品/视频点赞
  get_bidders: "/auction/get/page/goods",//根据当前商品ID下参与竞拍的用户
  commit_info:"/auction/put/auction",   // 竞拍确认出果冻
  bidder_info:"/auction/get/auction/detail",//查询竞拍信息
  getDefault_address:"/tuser/get/defaultAddress",//获取用户默认地址
  publish_express:"/order/put/mailApply",//申请寄件
  get_freight:"/goods/put/buy/freight",// 获取运费
  get_setting: "/setting/get",  //获取规则信息
  get_share_goods: "/goods/get/shareGoods",//获取分享海报信息
  get_collection:"/store/get/is",//检验是否收藏，>0已收藏，否则为未收藏
  get_share_groupbuy: "/groupbuy/get/shareGroup",//获取分享拼团商品图片地址
  task_put_shareWxGroup: "/task/put/shareWxGroup", //分享微信群
  get_official_account: "/brand/get/share",//分享公众号
  get_official_account_guide: "/brand/get/guideShare",//分享引导公众号,
  get_rob_time_list: "/goods/get/rob/time/list",//抢兑商品时间列表
  get_rob_list: "/goods/get/rob/list",//抢兑商品列表
  put_rob_buy: "/goods/put/rob/buy",//抢兑一口价商品下单
  save_share: "/wx/put/wxShareSave", // 存入微信分享记录
  choose_address: "/tuser/put/chooseAddress", //选择默认地址
  // 开心果冻
  noReceive: "/api/mammon/user_winning_collects",  //奖励未领取列表
  awardList: "/card-service-web/battleWin/queryBattleWinning",  //实物奖励列表
  getJelly: "/api/mammon/user_winning_collects/share",  // 领取果冻
  passWord_star: "/api/mammon/user_codes/rank",//密码之星
  past_List: "/api/mammon/periods/draw_weekly_record",//往期记录
  win_info: "/api/mammon/periods/user_win_info",
  homeInfo: "/api/mammon/periods",//果冻主页
  getRule: "/api/mammon/periods/activity_rule",//规则
  myPassword: '/api/mammon/user_codes/my_codes',//我的密码
  passwordRecord: '/api/mammon/user_codes',//密码记录
  battleAwards: '/api/mammon/periods/welfare',//竞赛奖励
  // 钱包
  competition_balance_detail: "/api/withdrawals_orders/balance_detail",//竞赛奖励明细
  competition_balance_list: "/api/withdrawals_orders/balance_list.json",//竞赛零钱明细
  // 提现
  get_cash_list: "/drawcash/list",     // 零钱提现信息列表  
  get_cash_obtaion: "/drawcash/operation", // 提现操作（兑换）
  get_user_amount_detail: "/drawcash/userAmountDetail",// 零钱明细
  get_user_focusUxuan: "/wx/get/userfocusUxuan",//关注U选公众号
  // 消息
  read_notice: "/notice/get/readNotice", // 	读取公告
  read_msg: "/sysmsg/update/readMsg", // 	读取系统消息
  userTotalAmount: "/drawcash/userTotalAmount", //提现总金额
};
/**
 * 网络请求封装
 * @param contentTypeJson 用于判断header中的"content-type"类型 必传
 * @param url url路径名 例：/books
 * @param method 请求方式 POST/GET/DELETE等
 * @param data 请求参数 string类型
 * @param success  成功回调
 * @param fail 失败回调
 */
function request(contentTypeJson, url, method, data, success, fail) {
  if (typeof contentTypeJson === "boolean") {

  } else {
    wx.showToast({
      title: contentTypeJson + '未传入contentType类型',
    })
    return
  }
  if (!fail && !success && typeof data === "function") {
    // fail = null;
    success = data;
    data = "";
  } else if (!fail) {
    if (typeof data === "function") {
      fail = success;
      success = data;
      data = "";
    } else if (typeof data === "object") {
      // fail = null
    } else {
      console.log("传递参数类型不正确");
    }
  }

  const App = getApp();
  const { token } = App.globalData;
  const { accessToken } = App.globalData;

  let header = {};
  if (contentTypeJson) {
    header = {
      "content-type": "application/json",
      "token": token,
      "accessToken": accessToken
    }
  } else {
    header = {
      "content-type": 'application/x-www-form-urlencoded',
      "token": token,
      "accessToken": accessToken
    }
  }
  wx.request({
    url: getApp().globalData.baseUrl + url,
    data: data,
    header: header,
    method: method,
    success: function (res) {
      console.log(res);
      switch (res.statusCode) {
        case 200:
          typeof success == "function" && success(res.data);
          break;

        default:
        //700://token验证失败 701://token过期
          if (res.data.code == 700 || res.data.code == 701){
            getApp().loginService()
          }
          typeof fail == "function" && fail(res);
          break;
      }
    },
    fail: function (res) {
      console.log(res);
      typeof fail == "function" && fail(res);
    }
  });
}

/**
 * 请求封装-Get
 * @param url 请求地址
 * @param data 请求参数
 * @param success 成功回调
 * @param fail  失败回调
 * @constructor
 *
 * 返回值为微信请求实例   用于取消请求
 */
function Get(contentTypeJson, url, data, success, fail) {
  return request(contentTypeJson, url, "GET", data, success, fail);
}

/**
 * 请求封装-Post
 * @param url 请求地址
 * @param data 请求参数
 * @param success 成功回调
 * @param fail  失败回调
 * @constructor
 *
 * 返回值为微信请求实例   用于取消请求
 */
function Post(contentTypeJson, url, data, success, fail) {
  return request(contentTypeJson, url, "POST", data, success, fail);
}

/**
 * 请求封装-Delete
 * @param url 请求地址
 * @param data 请求参数
 * @param success 成功回调
 * @param fail  失败回调
 * @constructor
 *
 * 返回值为微信请求实例   用于取消请求
 */
function Delete(contentTypeJson, url, data, success, fail) {
  return request(contentTypeJson, url, "DELETE", data, success, fail);
}

const Promise = require("./promise").Promise;
/**
 * http
 * @param {String} url 请求URL
 * @param {String} method 请求类型
 * @param {Object} data 请求数据
 */
function http(url, method, data) {
  if (typeof url === "object") {
    method = url.method;
    data = url.data;
    url = getApp().globalData.baseUrl + url.url;
  } else {
    url = getApp().globalData.baseUrl + url;
  }
  function wxRequest(resolve, reject) {
    wx.request({
      url: url,
      data: data,
      header: {
        "content-type": 'application/x-www-form-urlencoded'
      },
      method: method,
      success: function (res) {
        switch (res.statusCode) {
          case 200:
            resolve(res.data);
            break;
          default:
            reject(res);
            break;
        }
      },
      fail: function (res) {
        reject(res);
      }
    });
  }
  return Promise(wxRequest);
}

exports.http = http;
exports.Get = Get;
exports.Post = Post;
exports.Delete = Delete;
exports.api = api;
// 使用
/*

function request() {
    //POST/DELETE 请求方式调用方法一样

    //1、网络请求（没有请求参数，不需要对请求失败情况处理）
    request.Get('/classify', function (res) {
        console.log(res);
    })

    //2、网络请求（没有请求参数，需要对请求失败情况处理）
    request.Get('/classify', function (res) {
        console.log(res);
    }, function (err) {
        console.log(err);
    })

    //3、网络请求（有请求参数，不需要对请求失败情况处理）
    var data = {
        username: 'username',
        age: 19
    };
    request.Get('/classify', data, function (res) {
        console.log(res);
    })

    //4、网络请求（有请求参数，需要对请求失败情况处理）
    request.Get('/classify', data, function (res) {
        console.log(res);
    }, function (err) {
        console.log(err);
    })

    //5、取消网络请求(所有的请求方法均返回requestTask对象，可中断请求任务)
    var requestTask = request.Get('/classify', function (res) {
        console.log(res);
    });
    //网络请求取消
    requestTask.abort()
}
*/
